package com.example.wangning.mycarofcountry.forum.jingxuan;

import android.content.Context;
import android.graphics.Movie;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.mycarofcountry.entity.dao.JingXuanList;
import com.example.wangning.mycarofcountry.R;
import com.example.wangning.mycarofcountry.cache.StreamRequest;
import com.example.wangning.mycarofcountry.cache.VolleySingleton;
import com.example.wangning.mycarofcountry.tool.GifMovieView;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by dllo on 16/1/16.
 */
public class JingXuanAdapter extends BaseAdapter {
    private List<JingXuanList> data;
    private Context context;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    LayoutInflater inflater;
    final int VIEW_TYPE = 2;
    final int TYPE_1 = 0;
    final int TYPE_2 = 1;


    public JingXuanAdapter(Context context) {
        this.context = context;
        data = new ArrayList<>();
        volleySingleton = VolleySingleton.getVolleyIntance();
    }

    public void addData(List<JingXuanList> datas) {
        data = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        int p = Integer.valueOf(data.get(position).getIs_gif());
        if (p == 0) {
            return TYPE_1;
        } else {
            return TYPE_2;
        }

    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }
    //设置NetworkImage
    private void getBitmap(String url, final NetworkImageView networkImageView) {
         imageLoader = volleySingleton.getImageLoader();
        networkImageView.setDefaultImageResId(R.mipmap.cleak_repeat_normal);
        networkImageView.setErrorImageResId(R.mipmap.cleak_repeat_normal);
        networkImageView.setImageUrl(url, imageLoader);
    }
    //设置动态图
    private void getGifMovie(String url, final GifMovieView imageView) {

        StreamRequest request = new StreamRequest(url,
                new Response.Listener<InputStream>() {
                    @Override
                    public void onResponse(InputStream response) {
                        Movie movie = Movie.decodeStream(response);
                        imageView.setMovie(movie);
                        try {
                            response.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                // TODO Auto-generated method stub
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };


        volleySingleton.addRequest(request);//将本次图片请求加入到请求队列中
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder = null;
        OtherViewHolder otherViewHolder = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            inflater = LayoutInflater.from(context);
            switch (type) {
                case TYPE_1:
                    convertView = inflater.inflate(R.layout.list_tupian_view_main, parent, false);
                    myViewHolder = new MyViewHolder(convertView);
                    convertView.setTag(myViewHolder);
                    break;
                case TYPE_2:
                    convertView = inflater.inflate(R.layout.list_tupian_view_other, parent, false);
                    otherViewHolder = new OtherViewHolder(convertView);
                    convertView.setTag(otherViewHolder);
                    break;
            }

        } else {
            switch (type) {
                case TYPE_1:
                    myViewHolder = (MyViewHolder) convertView.getTag();
                    break;
                case TYPE_2:
                    otherViewHolder = (OtherViewHolder) convertView.getTag();
                    break;
            }
        }
        switch (type) {
            case TYPE_1:
                if (data != null && data.size() > 0) {
                    JingXuanList jingXuanList = data.get(position);
                    if (jingXuanList != null) {
                        myViewHolder.text.setText(jingXuanList.getText());
                        myViewHolder.created_at.setText(jingXuanList.getCreated_at());
                        myViewHolder.love.setText(jingXuanList.getLove());
                        myViewHolder.hate.setText(jingXuanList.getHate());
                        myViewHolder.name.setText(jingXuanList.getName());
                        myViewHolder.repost.setText(jingXuanList.getRepost());
                        myViewHolder.comment.setText(jingXuanList.getComment());
                        if (jingXuanList.getProfile_image() != null && jingXuanList.getProfile_image().length() > 0) {
                            String url = jingXuanList.getProfile_image();
                            volleySingleton.getBitmap(volleySingleton, url, myViewHolder.profile_image);
                        }
                        if (jingXuanList.getImage0() != null && jingXuanList.getImage0().length() > 0) {
                            String imgUrl = jingXuanList.getImage0();
                            getBitmap(imgUrl, myViewHolder.image0);
                        }
                    }
                }
                break;
            case TYPE_2:
                if (data != null && data.size() > 0) {
                    JingXuanList list = data.get(position);
                    if (list != null) {
                        otherViewHolder.text.setText(list.getText());
                        otherViewHolder.created_at.setText(list.getCreated_at());
                        otherViewHolder.hate.setText(list.getHate());
                        otherViewHolder.love.setText(list.getLove());
                        otherViewHolder.name.setText(list.getName());
                        otherViewHolder.repost.setText(list.getRepost());
                        otherViewHolder.comment.setText(list.getComment());
                        if (list.getProfile_image() != null && list.getProfile_image().length() > 0) {
                            String url = list.getProfile_image();
                            volleySingleton.getBitmap(volleySingleton, url, otherViewHolder.profile_image);
                        }
                        if (list.getImage0() != null && list.getImage0().length() > 0) {
                            String imgUrl = list.getImage0();
                            getGifMovie(imgUrl, otherViewHolder.image0);
                        }
                    }
                }
                break;
        }

        return convertView;
    }

    class MyViewHolder {
        TextView text, name, created_at, love, hate,repost,comment;
        ImageView profile_image;
        NetworkImageView image0;

        public MyViewHolder(View view) {
            text = (TextView) view.findViewById(R.id.textTv);
            name = (TextView) view.findViewById(R.id.nameTv);
            created_at = (TextView) view.findViewById(R.id.created_atTv);
            love = (TextView) view.findViewById(R.id.loveTv);
            hate = (TextView) view.findViewById(R.id.hateTv);
            repost = (TextView) view.findViewById(R.id.repostTv);
            comment = (TextView) view.findViewById(R.id.commentTv);
            profile_image = (ImageView) view.findViewById(R.id.profile_image);
            image0 = (NetworkImageView) view.findViewById(R.id.image0);
        }
    }

    class OtherViewHolder {
        TextView text, name, created_at, love, hate,repost,comment;
        ImageView profile_image;
        GifMovieView image0;

        public OtherViewHolder(View view) {
            text = (TextView) view.findViewById(R.id.textTv);
            name = (TextView) view.findViewById(R.id.nameTv);
            created_at = (TextView) view.findViewById(R.id.created_atTv);
            love = (TextView) view.findViewById(R.id.loveTv);
            hate = (TextView) view.findViewById(R.id.hateTv);
            repost = (TextView) view.findViewById(R.id.repostTv);
            comment = (TextView) view.findViewById(R.id.commentTv);
            profile_image = (ImageView) view.findViewById(R.id.profile_image);
            image0 = (GifMovieView) view.findViewById(R.id.image0);

        }
    }


}
